//
//  PPAlbumContentTagsViewController.m
//  pickpic
//
//  Created by Jinxiao on 9/12/16.
//  Copyright © 2016 MicroDream. All rights reserved.
//

#import "PPAlbumContentTagsViewController.h"

#import "PPAlbumContentViewDataSource.h"

#import "PPNetworkService.h"

#import "PPAlbumContentTagsTableHeaderView.h"
#import "PPAlbumContentTagsTableFooterView.h"

#import "PPAlbumHotTagModel.h"
#import "PPAlbumCustomTagModel.h"

@interface PPAlbumContentTagsViewController () <UITableViewDelegate, UITableViewDataSource, PPAlbumContentTagsTableHeaderViewDelegate, PPAlbumContentTagsTableFooterViewDelegate>
@property (readwrite, nonatomic, strong) PPAlbumContentTagsTableHeaderView *headerView;
@property (readwrite, nonatomic, strong) PPAlbumContentTagsTableFooterView *footerView;
@property (readwrite, nonatomic, strong) UITableView *tableView;
@property (readwrite, nonatomic, strong) NSMutableArray <PPAlbumTagElement *> *tags;
@end

@implementation PPAlbumContentTagsViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"标签";

    _tags = [[NSMutableArray alloc] init];

    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 15, self.view.width, self.view.height - 15) style:UITableViewStyleGrouped];
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];

    __weak typeof(self)weakSelf = self;
    [self setNavigationLeftTitle:@"取消" action:^{
        [weakSelf.navigationController popViewControllerAnimated:YES];
    }];

    [self setNavigationRightTitle:@"完成" titleColor:[UIColor colorWithRGBHex:0xffdd17] action:^{
        weakSelf.dataSource.tags = weakSelf.tags;
        [weakSelf.navigationController popViewControllerAnimated:YES];
    }];

    _footerView = [[PPAlbumContentTagsTableFooterView alloc] init];
    _footerView.frame = CGRectMake(0, 20, self.view.width, 60);
    _footerView.delegate = self;

    NSMutableArray *hotTags = [NSMutableArray array];
    [[PPAlbumHotTagModel all] enumerateObjectsUsingBlock:^(PPAlbumHotTagModel *_Nonnull obj, NSUInteger idx, BOOL *_Nonnull stop) {
        PPAlbumTagElement *tag = [[PPAlbumTagElement alloc] init];
        tag.name = obj.name;
        tag.tagID =  obj.tagID;
        tag.isHot = YES;
        [hotTags addObject:tag];
    }];
    _footerView.hotTags = hotTags;

    NSMutableArray *customTags = [NSMutableArray array];
    [[PPAlbumCustomTagModel all] enumerateObjectsUsingBlock:^(PPAlbumCustomTagModel *_Nonnull obj, NSUInteger idx, BOOL *_Nonnull stop) {
        PPAlbumTagElement *tag = [[PPAlbumTagElement alloc] init];
        tag.name = obj.name;
        tag.tagID = obj.tagID;
        [customTags addObject:tag];
    }];
    _footerView.customTags = customTags;

    [_footerView reloadData];
    _tableView.tableFooterView = _footerView;

    _headerView = [[PPAlbumContentTagsTableHeaderView alloc] init];
    _headerView.frame = CGRectMake(0, 20, self.view.width, 60);
    _headerView.delegate = self;
    _tableView.tableHeaderView = _headerView;

    [_dataSource.tags enumerateObjectsUsingBlock:^(PPAlbumTagElement *_Nonnull obj, NSUInteger idx, BOOL *_Nonnull stop) {
        [_headerView addTag:obj];
    }];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    _tableView.frame = CGRectMake(0, 15, self.view.width, self.view.height - 15);
}

#pragma mark - UITableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return nil;
}

#pragma mark - PPAlbumContentTagsTableHeaderViewDelegate

- (id)headerView:(PPAlbumContentTagsTableHeaderView *)headerView didAddTag:(PPAlbumTagElement *)tag {
    [_tags addObject:tag];

    [_footerView selectTag:tag];

    return tag;
}

- (void)headerView:(PPAlbumContentTagsTableHeaderView *)headerView didRemoveTag:(PPAlbumTagElement *)tag {
    NSMutableArray *deleteTags = [NSMutableArray array];
    [_tags enumerateObjectsUsingBlock:^(PPAlbumTagElement *_Nonnull obj, NSUInteger idx, BOOL *_Nonnull stop) {
        if([obj.name isEqualToString:tag.name]) {
            [deleteTags addObject:obj];
        }
    }];
    [_tags removeObjectsInArray:deleteTags];

    [_footerView deselectTag:tag];
}

- (void)headerViewDidUpdateFrame:(PPAlbumContentTagsTableHeaderView *)headerView {

    _tableView.tableHeaderView = headerView;

}

#pragma mark - PPAlbumContentTagsTableFooterViewDelegate

- (void)footerView:(PPAlbumContentTagsTableFooterView *)footerView didSelectTag:(PPAlbumTagElement *)tag {
    [_headerView addTag:tag];
}

- (void)footerView:(PPAlbumContentTagsTableFooterView *)footerView didDeselectTag:(PPAlbumTagElement *)tag {
    [_headerView removeTag:tag];
}

@end
